﻿using System;
using System.Collections.ObjectModel;

namespace DCT.ViewModel
{
    public class WorkspaceManager : ViewModelBase
    {
        #region Constructor

        private WorkspaceManager()
        {
            _workspaces = new ObservableCollection<WorkspaceViewModel>();
        }

        #endregion //Constructor

        #region Fields

        private WorkspaceViewModel _activeWorkspace;
        private ObservableCollection<WorkspaceViewModel> _workspaces;

        #endregion //Fields

        #region Properties

        // singleton
        private static WorkspaceManager _instance;
        public static WorkspaceManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new WorkspaceManager();
                }

                return _instance;
            }
        }

        public WorkspaceViewModel ActiveWorkspace
        {
            get 
            { 
                return _activeWorkspace; 
            }
            set
            {
                if (_activeWorkspace != value)
                {
                    //if (value == null && this.Workspaces.Count == 1)
                    //    throw new InvalidOperationException("Cannot set the active document to null when there are still open documents");

                    WorkspaceViewModel previouslyActiveWorkspace = _activeWorkspace;

                    _activeWorkspace = value;
                    base.OnPropertyChanged("ActiveWorkspace");

                    if (value != null && this.Workspaces.Contains(value) == false)
                    {
                        this._workspaces.Add(value);
                    }

                    if (previouslyActiveWorkspace != null)
                    {
                        previouslyActiveWorkspace.UnActivated();
                    }

                    if (value != null)
                    {
                        value.Activated();
                    }

                    if (this.ActiveDocumentChanged != null)
                    {
                        this.ActiveDocumentChanged(this, new ActiveWorkspaceChangedEventArgs(previouslyActiveWorkspace, value));
                    }
                }
            }
        }

        public ObservableCollection<WorkspaceViewModel> Workspaces
        {
            get
            {
                return _workspaces;
            }
        }

        public int Count
        {
            get { return _workspaces.Count; }
        }

        #endregion //Properties

        #region Events

        public event EventHandler<ActiveWorkspaceChangedEventArgs> ActiveDocumentChanged;

        #endregion

        #region Methods

        public bool Contains(WorkspaceViewModel workspaceViewModel)
        {
            return _workspaces.Contains(workspaceViewModel);
        }

        public void Open(WorkspaceViewModel workspaceViewModel)
        {
            if (!this.Workspaces.Contains(workspaceViewModel))
            {
                _workspaces.Add(workspaceViewModel);

                if (_workspaces.Count == 1)
                {
                    this.ActiveWorkspace = workspaceViewModel;
                }
            }
        }

        public void Close(WorkspaceViewModel workspaceViewModel)
        {
            bool wasClosed = _workspaces.Remove(workspaceViewModel);

            if (this.ActiveWorkspace == workspaceViewModel)
            {
                this.ActiveWorkspace = this.Workspaces.Count == 0 ? null : this.Workspaces[0];
            }
        }

        #endregion
    }
}
